#include "client.h"

int recvfile(int sockfd, char* pathname){
    //注册信号用于暂停下载
    current_statu = 1;
    fd = open(pathname,O_RDWR|O_CREAT|O_APPEND,0666);
//    //计算映射偏移量并发送给传输方，映射偏移量必须是4096整数倍
    offset = lseek(fd,0,SEEK_END);//文件长度
    offset = offset - (offset%4096);//调整为4096整数倍，实际偏移量比文件长度略小
    send(sockfd,&offset,sizeof(offset),MSG_NOSIGNAL);
    recv(sockfd,&filesize,sizeof(off_t),0);
//    printf("filesize = %ld\n", filesize);
    mmap_size = filesize - offset;
    ftruncate(fd, filesize);
    mp  = (char *)mmap(NULL,mmap_size,PROT_READ|PROT_WRITE,MAP_SHARED,fd,offset);
    recvn(sockfd,mp,mmap_size);
    munmap(mp,mmap_size);    
    close(fd);
    cursize = 0;
    current_statu = 0;
    return 0;
}
